﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace DONGTAIWANGYE
{
    public class SqlHelper
    {
        private static string strCn = "server=pc.hstc.cn;database=dhd2014112117;uid=2014112117;pwd=19960218";

        public static SqlDataReader ExecuteReader(string strSql, params SqlParameter[] pms)
        {
            SqlDataReader dr = null;
            SqlConnection cn = new SqlConnection(strCn);
            cn.Open();
            using (SqlCommand cmd = new SqlCommand(strSql, cn))
            {
                if (pms != null) cmd.Parameters.AddRange(pms);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return dr;
        }

        public static object ExecuteScalar(string strSql, params SqlParameter[] pms)
        {
            object obj = null;
            using (SqlConnection cn = new SqlConnection(strCn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(strSql, cn))
                {
                    if (pms != null) cmd.Parameters.AddRange(pms);
                    obj = cmd.ExecuteScalar();
                }
            }
            return obj;
        }

        public static int ExecuteNonQuery(string strSql, params SqlParameter[] pms)
        {
            int iCount = 0;
            using (SqlConnection cn = new SqlConnection(strCn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(strSql, cn))
                {
                    if (pms != null) cmd.Parameters.AddRange(pms);
                    iCount = cmd.ExecuteNonQuery();
                }
            }
            return iCount;
        }
    }
}